package org.cnc.explain.lifecycle;

import org.cnc.explain.util.StepCount;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.util.ReaderHelper;

/**
 * org.cnc.spring.explain.lifecycle - LifeCycleBeanPostProcessor
 *
 * @author tony-is-coding
 * @date 2022/7/4 17:47
 */
@Component
public class LifeCycleBeanPostProcessor implements BeanPostProcessor{

	@Override
	@SuppressWarnings(" NullableProblems")
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if(LifeCycleBean.BEAN_NAME.equals(beanName)){
			System.out.println("\n\n");
			ReaderHelper.beanLifecycleTrace ( StepCount.getStep() + "bean [%s] 初始化前置: " ,beanName);
			ReaderHelper.formatTrace();
			System.out.println("\n\n");
		}
		return bean;
	}

	@Override
	@SuppressWarnings(" NullableProblems")
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if(LifeCycleBean.BEAN_NAME.equals(beanName)){
			System.out.println("\n\n");
			ReaderHelper.beanLifecycleTrace ( StepCount.getStep() + "bean [%s] 初始化后置: " ,beanName);
			ReaderHelper.formatTrace();
			System.out.println("\n\n");
		}
		return bean;
	}


}
